package com.qms.qep.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qms.common.core.text.Convert;
import com.qms.common.exception.ServiceException;
import com.qms.common.utils.StringUtils;
import com.qms.qep.mapper.ProductCategoryMapper;
import com.qms.qep.domain.ProductCategory;
import com.qms.qep.service.IProductCategoryService;

/**
 * 产品分类Service业务层处理
 * 
 * @author qep
 */
@Service
public class ProductCategoryServiceImpl implements IProductCategoryService 
{
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 查询产品分类
     * 
     * @param id 产品分类主键
     * @return 产品分类
     */
    @Override
    public ProductCategory selectProductCategoryById(Long id)
    {
        return productCategoryMapper.selectProductCategoryById(id);
    }

    /**
     * 查询产品分类列表
     * 
     * @param productCategory 产品分类
     * @return 产品分类
     */
    @Override
    public List<ProductCategory> selectProductCategoryList(ProductCategory productCategory)
    {
        return productCategoryMapper.selectProductCategoryList(productCategory);
    }

    /**
     * 新增产品分类
     * 
     * @param productCategory 产品分类
     * @return 结果
     */
    @Override
    public int insertProductCategory(ProductCategory productCategory)
    {
        return productCategoryMapper.insertProductCategory(productCategory);
    }

    /**
     * 修改产品分类
     * 
     * @param productCategory 产品分类
     * @return 结果
     */
    @Override
    public int updateProductCategory(ProductCategory productCategory)
    {
        return productCategoryMapper.updateProductCategory(productCategory);
    }

    /**
     * 批量删除产品分类
     * 
     * @param ids 需要删除的产品分类主键
     * @return 结果
     */
    @Override
    public int deleteProductCategoryByIds(Long[] ids)
    {
        for (Long id : ids)
        {
            ProductCategory productCategory = selectProductCategoryById(id);
            if (productCategoryMapper.selectProductCategoryCountChildren(id) > 0)
            {
                throw new ServiceException(String.format("%1$s存在子分类,不允许删除", productCategory.getName()));
            }
        }
        return productCategoryMapper.deleteProductCategoryByIds(ids);
    }

    /**
     * 删除产品分类信息
     * 
     * @param id 产品分类主键
     * @return 结果
     */
    @Override
    public int deleteProductCategoryById(Long id)
    {
        ProductCategory productCategory = selectProductCategoryById(id);
        if (productCategoryMapper.selectProductCategoryCountChildren(id) > 0)
        {
            throw new ServiceException(String.format("%1$s存在子分类,不允许删除", productCategory.getName()));
        }
        return productCategoryMapper.deleteProductCategoryById(id);
    }
    
    /**
     * 构建前端所需要的产品分类树结构
     * 
     * @param productCategories 产品分类列表
     * @return 树结构列表
     */
    @Override
    public List<ProductCategory> buildProductCategoryTree(List<ProductCategory> productCategories)
    {
        List<ProductCategory> returnList = new ArrayList<ProductCategory>();
        List<Long> tempList = productCategories.stream().map(ProductCategory::getId).collect(Collectors.toList());
        for (ProductCategory productCategory : productCategories)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(productCategory.getParentId()))
            {
                recursionFn(productCategories, productCategory);
                returnList.add(productCategory);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = productCategories;
        }
        return returnList;
    }
    
    /**
     * 查询产品分类树列表
     * 
     * @return 所有产品分类信息
     */
    @Override
    public List<ProductCategory> selectProductCategoryTreeList()
    {
        List<ProductCategory> productCategories = productCategoryMapper.selectProductCategoryTreeList();
        return buildProductCategoryTree(productCategories);
    }
    
    /**
     * 递归列表
     */
    private void recursionFn(List<ProductCategory> list, ProductCategory t)
    {
        // 得到子节点列表
        List<ProductCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ProductCategory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<ProductCategory> getChildList(List<ProductCategory> list, ProductCategory t)
    {
        List<ProductCategory> tlist = new ArrayList<ProductCategory>();
        Iterator<ProductCategory> it = list.iterator();
        while (it.hasNext())
        {
            ProductCategory n = (ProductCategory) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ProductCategory> list, ProductCategory t)
    {
        return getChildList(list, t).size() > 0;
    }
} 